package com.ipDev.moyvSys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ipDev.moyvSys.constant.StatusConstant;
import com.ipDev.moyvSys.domain.dto.EmpLoginDTO;
import com.ipDev.moyvSys.domain.po.Employee;
import com.ipDev.moyvSys.mapper.EmployeeMapper;
import com.ipDev.moyvSys.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 职工表 服务实现类
 * </p>
 *
 * @author fish
 * @since 2024-10-31
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Override
    public Employee login(EmpLoginDTO empLoginDTO) {
        String empName = empLoginDTO.getEmpName();

        //允许管理员作为普通职工登录 不允许普通职工作为管理员登录
        Integer admin = empLoginDTO.getAdmin();

        Employee emp = lambdaQuery().eq(Employee::getEmpName, empName).eq(admin==0,Employee::getAdmin,0).one();
        if (BeanUtil.isEmpty(emp)) {
            log.error("职工不存在!");
            return null;
        }
        if (emp.getStatus().equals(StatusConstant.DISABLE)) {
            log.error("职工账号被禁用!");
            return null;
        }
        if (!emp.getPassword().equals(empLoginDTO.getPassword())) {
            log.error("职工名或密码错误!");
            return null;
        }
        return emp;
    }
}
